package health

import (
	"gitcode.com/G-YT/ANN/server/global"
	"gitcode.com/G-YT/ANN/server/model/common/request"
	"gitcode.com/G-YT/ANN/server/model/common/response"
	"gitcode.com/G-YT/ANN/server/model/health"
	healthReq "gitcode.com/G-YT/ANN/server/model/health/request"
	"gitcode.com/G-YT/ANN/server/service"
	"gitcode.com/G-YT/ANN/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type HealthQuestionApi struct {
}

var hqService = service.ServiceGroupApp.HealthServiceGroup.HealthQuestionService

// CreateHealthQuestion 創建健康问答-待回答问题表
// @Tags HealthQuestion
// @Summary 創建健康问答-待回答问题表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body health.HealthQuestion true "創建健康问答-待回答问题表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"創建成功"}"
// @Router /hq/createHealthQuestion [post]
func (hqApi *HealthQuestionApi) CreateHealthQuestion(c *gin.Context) {
	var hq health.HealthQuestion
	err := c.ShouldBindJSON(&hq)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Uid":      {utils.NotEmpty()},
		"Question": {utils.NotEmpty()},
	}
	if err := utils.Verify(hq, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := hqService.CreateHealthQuestion(&hq); err != nil {
		global.GVA_LOG.Error("創建失敗!", zap.Error(err))
		response.FailWithMessage("創建失敗", c)
	} else {
		response.OkWithMessage("創建成功", c)
	}
}

// DeleteHealthQuestion 删除健康问答-待回答问题表
// @Tags HealthQuestion
// @Summary 删除健康问答-待回答问题表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body health.HealthQuestion true "删除健康问答-待回答问题表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /hq/deleteHealthQuestion [delete]
func (hqApi *HealthQuestionApi) DeleteHealthQuestion(c *gin.Context) {
	var hq health.HealthQuestion
	err := c.ShouldBindJSON(&hq)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := hqService.DeleteHealthQuestion(hq); err != nil {
		global.GVA_LOG.Error("删除失敗!", zap.Error(err))
		response.FailWithMessage("删除失敗", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteHealthQuestionByIds 批量删除健康问答-待回答问题表
// @Tags HealthQuestion
// @Summary 批量删除健康问答-待回答问题表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除健康问答-待回答问题表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /hq/deleteHealthQuestionByIds [delete]
func (hqApi *HealthQuestionApi) DeleteHealthQuestionByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := hqService.DeleteHealthQuestionByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失敗!", zap.Error(err))
		response.FailWithMessage("批量删除失敗", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateHealthQuestion 更新健康问答-待回答问题表
// @Tags HealthQuestion
// @Summary 更新健康问答-待回答问题表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body health.HealthQuestion true "更新健康问答-待回答问题表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /hq/updateHealthQuestion [put]
func (hqApi *HealthQuestionApi) UpdateHealthQuestion(c *gin.Context) {
	var hq health.HealthQuestion
	err := c.ShouldBindJSON(&hq)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Uid":      {utils.NotEmpty()},
		"Question": {utils.NotEmpty()},
	}
	if err := utils.Verify(hq, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := hqService.UpdateHealthQuestion(hq); err != nil {
		global.GVA_LOG.Error("更新失敗!", zap.Error(err))
		response.FailWithMessage("更新失敗", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindHealthQuestion 用id査詢健康问答-待回答问题表
// @Tags HealthQuestion
// @Summary 用id査詢健康问答-待回答问题表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query health.HealthQuestion true "用id査詢健康问答-待回答问题表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"査詢成功"}"
// @Router /hq/findHealthQuestion [get]
func (hqApi *HealthQuestionApi) FindHealthQuestion(c *gin.Context) {
	var hq health.HealthQuestion
	err := c.ShouldBindQuery(&hq)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if rehq, err := hqService.GetHealthQuestion(hq.ID); err != nil {
		global.GVA_LOG.Error("査詢失敗!", zap.Error(err))
		response.FailWithMessage("査詢失敗", c)
	} else {
		response.OkWithData(gin.H{"rehq": rehq}, c)
	}
}

// GetHealthQuestionList 分页獲取健康问答-待回答问题表列表
// @Tags HealthQuestion
// @Summary 分页獲取健康问答-待回答问题表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query healthReq.HealthQuestionSearch true "分页獲取健康问答-待回答问题表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /hq/getHealthQuestionList [get]
func (hqApi *HealthQuestionApi) GetHealthQuestionList(c *gin.Context) {
	var pageInfo healthReq.HealthQuestionSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := hqService.GetHealthQuestionInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "獲取成功", c)
	}
}
